package com.avic.rong.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.bean.HomeBean;
import com.avic.rong.listener.CriterionItemOnClickListener;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.minato.minastore.utils.AppUtils;
import com.example.minato.minastore.utils.GlideApp;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Creater: Created by wangyz on 2017/1/10.
 * Email: 936804097@qq.com
 */

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.RecommandViewHolder> {


    private Context mContext;
    private List<HomeBean.CriterionBean> data = new ArrayList<>();

    public BookAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<HomeBean.CriterionBean> list){
        this.data = list;
        notifyDataSetChanged();
    }

    @Override
    public BookAdapter.RecommandViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.view_book, parent, false);
        RecommandViewHolder holder = new RecommandViewHolder(view, viewType);

        return holder;
    }

    @Override
    public void onBindViewHolder(BookAdapter.RecommandViewHolder holder, final int position) {

        GlideApp.with(mContext).load(AppUtils.IMG2_URL + data.get(position).getTargetImg())
                .addListener(new EmptyBitmapListener(holder,data.get(position)))
                .placeholder(R.mipmap.empty)
                .into(holder.img_category);

        holder.tv_category_name.setText(data.get(position).getTargetName());
        holder.tv_category_price.setText("¥" + data.get(position).getPrice());
        holder.itemView.setOnClickListener(new CriterionItemOnClickListener(mContext, AppUtils.BASE_URL + "app/good/info/" + data.get(position).getSkuId() + ".html"));

    }

    @Override
    public int getItemCount() {
        return data.size();
    }



    class EmptyBitmapListener implements RequestListener<Drawable> {
        private RecommandViewHolder holder;
        private HomeBean.CriterionBean recommand;

        public EmptyBitmapListener(RecommandViewHolder holder,HomeBean.CriterionBean recommand) {
            this.holder = holder;
            this.recommand = recommand;
        }

        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

            if (!TextUtils.isEmpty(recommand.getTargetCatName())){
                holder.tv_content.setVisibility(View.VISIBLE);
                holder.tv_content.setText(recommand.getTargetCatName());
            }
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            return false;
        }
    }


    class RecommandViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_category_name)
        TextView tv_category_name;
        @BindView(R.id.img_category)
        ImageView img_category;
        @BindView(R.id.tv_category_price)
        TextView tv_category_price;
        @BindView(R.id.tv_content)
        TextView tv_content;//无图提示

        public RecommandViewHolder(View itemView, int type) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
